package no.ntnu.item.ttm4115.termproject.playergameagent.gui;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;
import java.util.Map.Entry;

import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import no.ntnu.item.ttm4115.termproject.CoordsFromGUI;
import no.ntnu.item.ttm4115.termproject.FinalAnswerFromGUI;
import no.ntnu.item.ttm4115.termproject.StatsAnswerFromGUI;
import no.ntnu.item.ttm4115.termproject.extrasagent.SessionOption;
import no.ntnu.item.ttm4115.termproject.playeragent.gui.PlayerGameGUI;
import no.tellu.common.actorframe.ActorSM;

public class GameGUI extends JPanel{

    private javax.swing.JScrollPane jScrollPaneMemberStats;
    private javax.swing.JScrollPane jScrollPaneQuestionText;
    private javax.swing.JPanel jPanelQuestions;
    private javax.swing.JPanel jPanelMembersAnswers;
    private javax.swing.JPanel jPanelAnswerSession;
    private javax.swing.JLabel jLabelMember1;
    private javax.swing.JLabel jLabelMember2;
    private javax.swing.JLabel jLabelMember3;
    private javax.swing.JLabel jLabelMember4;
    private javax.swing.JLabel jLabelLeader;
    private javax.swing.JLabel jLabelOption1Stat;
    private javax.swing.JLabel jLabelOption2Stat;
    private javax.swing.JLabel jLabelOption3Stat;
    private javax.swing.JLabel jLabelOption4Stat;
    private javax.swing.JRadioButton jRadioOption1;
    private javax.swing.JRadioButton jRadioOption2;
    private javax.swing.JRadioButton jRadioOption3;
    private javax.swing.JRadioButton jRadioOption4;
	private ButtonGroup buttonGroupOptions;
    private javax.swing.JList jListMemberStats;
    private javax.swing.JButton jButtonFinalAnswer;
    private javax.swing.JButton jButtonPartialAnswer;
    private javax.swing.JTextArea jTextAreaQuestionText;
    private javax.swing.JComboBox jComboBoxStat;
    private javax.swing.DefaultListModel jMembersStatsModel;
    private PlayerGameGUI gameGUI;
    private ActorSM asm;
    private String tooltip1;
    private String tooltip2;
    private String tooltip3;
    private String tooltip4;
	
    
	
	
	
	public GameGUI(PlayerGameGUI gameGUI, ActorSM asm){
		
		this.gameGUI = gameGUI;
		this.gameGUI.getGameTabbePane().addTab("Treasure Found", this);
		this.repaint();
	    this.gameGUI.repaint();
	    this.asm = asm;

		
		initComponents();
	}
	
	
	private void initComponents() {
		
	tooltip1 = new String("<HTML><B>Following answers have been submited:</B><BR><BR>");	
	tooltip2 = new String("<HTML><B>Following answers have been submited:</B><BR><BR>");	
	tooltip3 = new String("<HTML><B>Following answers have been submited:</B><BR><BR>");	
	tooltip4 = new String("<HTML><B>Following answers have been submited:</B><BR><BR>");	
    jRadioOption1 = new javax.swing.JRadioButton();
    jRadioOption2 = new javax.swing.JRadioButton();
    jRadioOption3 = new javax.swing.JRadioButton();
    jRadioOption4 = new javax.swing.JRadioButton();
    jLabelOption1Stat = new javax.swing.JLabel();
    jLabelOption2Stat = new javax.swing.JLabel();
    jLabelOption3Stat = new javax.swing.JLabel();
    jLabelOption4Stat = new javax.swing.JLabel();
    jLabelMember1 = new javax.swing.JLabel();
    jLabelMember2 = new javax.swing.JLabel();
    jLabelMember3 = new javax.swing.JLabel();
    jLabelMember4 = new javax.swing.JLabel();
    jLabelLeader = new javax.swing.JLabel();
    jScrollPaneMemberStats = new javax.swing.JScrollPane();
    jScrollPaneQuestionText = new javax.swing.JScrollPane();
    jListMemberStats = new javax.swing.JList();
    jTextAreaQuestionText = new javax.swing.JTextArea();
    jButtonFinalAnswer = new javax.swing.JButton();
    jButtonPartialAnswer = new javax.swing.JButton();
    jComboBoxStat = new javax.swing.JComboBox();
    jPanelMembersAnswers = new javax.swing.JPanel();
    jPanelAnswerSession = new javax.swing.JPanel();
    jPanelQuestions = new javax.swing.JPanel();
    jMembersStatsModel = new DefaultListModel();

    
    jPanelQuestions.setBorder(javax.swing.BorderFactory.createTitledBorder("Questions"));

    jRadioOption1.setText("                                                                                 ");
    getButtonGroupOptions().add(jRadioOption1);
    jRadioOption2.setText("                                                                                 ");
    getButtonGroupOptions().add(jRadioOption2);
    jRadioOption3.setText("                                                                                     ");
    getButtonGroupOptions().add(jRadioOption3);
    jRadioOption4.setText("                                                                                ");
    getButtonGroupOptions().add(jRadioOption4);

    jScrollPaneQuestionText.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

    jTextAreaQuestionText.setColumns(20);
    jTextAreaQuestionText.setEditable(false);
    jTextAreaQuestionText.setLineWrap(true);
    jTextAreaQuestionText.setRows(5);
    jScrollPaneQuestionText.setViewportView(jTextAreaQuestionText);

    //edited by surya
    jButtonPartialAnswer.setText("Submit Choice");
    jButtonPartialAnswer.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButtonPartialAnswerActionPerformed(evt);
        }
    });
    
    jLabelOption1Stat.setText("0%");

    jLabelOption2Stat.setText("0%");

    jLabelOption3Stat.setText("0%");

    jLabelOption4Stat.setText("0%");
    
    jLabelOption1Stat.setToolTipText("No one have answered this alternative");
    
    jLabelOption2Stat.setToolTipText("No one have answered this alternative");
    
    jLabelOption3Stat.setToolTipText("No one have answered this alternative");
    
    jLabelOption4Stat.setToolTipText("No one have answered this alternative");

    
    jComboBoxStat.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Choose %", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100" }));

    jLabelMember4.setText("                              ");

    jLabelMember1.setText("                                                                                  ");

    jLabelMember2.setText("                                                             ");

    jLabelMember3.setText("                                                                             ");

    javax.swing.GroupLayout jPanelQuestionsLayout = new javax.swing.GroupLayout(jPanelQuestions);
    jPanelQuestions.setLayout(jPanelQuestionsLayout);
    jPanelQuestionsLayout.setHorizontalGroup(
        jPanelQuestionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanelQuestionsLayout.createSequentialGroup()
            .addGroup(jPanelQuestionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPaneQuestionText, javax.swing.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE)
                .addGroup(jPanelQuestionsLayout.createSequentialGroup()
                    .addComponent(jComboBoxStat, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanelQuestionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanelQuestionsLayout.createSequentialGroup()
                            .addGap(18, 18, 18)
                            .addComponent(jButtonPartialAnswer, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelQuestionsLayout.createSequentialGroup()
                            .addGap(178, 178, 178)
                            .addGroup(jPanelQuestionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabelOption3Stat, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE)
                                .addComponent(jLabelOption4Stat, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE)
                                .addComponent(jLabelOption2Stat, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE)
                                .addComponent(jLabelOption1Stat, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))))
                .addComponent(jLabelMember1)
                .addComponent(jLabelMember2)
                .addComponent(jLabelMember3)
                .addComponent(jLabelMember4)
                .addGroup(jPanelQuestionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jRadioOption4, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addComponent(jRadioOption3, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addComponent(jRadioOption1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 236, Short.MAX_VALUE)
                    .addComponent(jRadioOption2, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)))
            .addContainerGap())
    );
    jPanelQuestionsLayout.setVerticalGroup(
        jPanelQuestionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanelQuestionsLayout.createSequentialGroup()
            .addComponent(jScrollPaneQuestionText, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(jPanelQuestionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(jRadioOption1)
                .addComponent(jLabelOption1Stat, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(jPanelQuestionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabelOption2Stat, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jRadioOption2))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(jPanelQuestionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jRadioOption3)
                .addComponent(jLabelOption3Stat, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(jPanelQuestionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jRadioOption4)
                .addComponent(jLabelOption4Stat, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(39, 39, 39)
            .addComponent(jLabelMember1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jLabelMember2)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
            .addComponent(jLabelMember3)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jLabelMember4)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(jPanelQuestionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jComboBoxStat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButtonPartialAnswer))
            .addGap(16, 16, 16))
    );

    jPanelMembersAnswers.setBorder(javax.swing.BorderFactory.createTitledBorder("Members Answers"));

    jListMemberStats.setModel(getJMembersStatsModel());
    jScrollPaneMemberStats.setViewportView(jListMemberStats);

    javax.swing.GroupLayout jPanelMembersAnswersLayout = new javax.swing.GroupLayout(jPanelMembersAnswers);
    jPanelMembersAnswers.setLayout(jPanelMembersAnswersLayout);
    jPanelMembersAnswersLayout.setHorizontalGroup(
        jPanelMembersAnswersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanelMembersAnswersLayout.createSequentialGroup()
            .addComponent(jScrollPaneMemberStats, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
            .addContainerGap())
    );
    jPanelMembersAnswersLayout.setVerticalGroup(
        jPanelMembersAnswersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanelMembersAnswersLayout.createSequentialGroup()
            .addComponent(jScrollPaneMemberStats, javax.swing.GroupLayout.PREFERRED_SIZE, 322, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(116, Short.MAX_VALUE))
    );

    //edited by surya
    jButtonFinalAnswer.setText("Final Answer");
/*    jButtonFinalAnswer.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButtonFinalAnswerActionPerformed(evt);
        }
    });*/

    jLabelLeader.setText("");

    javax.swing.GroupLayout jPanelAnswerSessionLayout = new javax.swing.GroupLayout(this);
    this.setLayout(jPanelAnswerSessionLayout);
    jPanelAnswerSessionLayout.setHorizontalGroup(
        jPanelAnswerSessionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanelAnswerSessionLayout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanelAnswerSessionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanelAnswerSessionLayout.createSequentialGroup()
                    .addComponent(jPanelQuestions, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jPanelMembersAnswers, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(11, Short.MAX_VALUE))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelAnswerSessionLayout.createSequentialGroup()
                    .addComponent(jLabelLeader, javax.swing.GroupLayout.PREFERRED_SIZE, 372, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 73, Short.MAX_VALUE)
                    .addComponent(jButtonFinalAnswer, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(24, 24, 24))))
    );
    jPanelAnswerSessionLayout.setVerticalGroup(
        jPanelAnswerSessionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanelAnswerSessionLayout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanelAnswerSessionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                .addComponent(jPanelQuestions, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanelMembersAnswers, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGap(11, 11, 11)
            .addGroup(jPanelAnswerSessionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jButtonFinalAnswer)
                .addComponent(jLabelLeader))
            .addContainerGap())
    );

    jButtonFinalAnswer.addActionListener(new java.awt.event.ActionListener() {
    	public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButtonFinalAnswerActionPerformed(evt);
        }
    });
    
    }
    
	private ButtonGroup getButtonGroupOptions() {
		if(buttonGroupOptions == null) {
			buttonGroupOptions = new ButtonGroup();
		}
		return buttonGroupOptions;
	}
	
	private void jButtonPartialAnswerActionPerformed(java.awt.event.ActionEvent evt) {
		int i = 7;
		if(jRadioOption1.isSelected()){
			i = 0;
		}
		if(jRadioOption2.isSelected()){
			i = 1;	
		}
		if(jRadioOption3.isSelected()){
			i = 2;
		}
		if(jRadioOption4.isSelected()){
			i = 3;
		}
		String statValueString = (String) jComboBoxStat.getSelectedItem();
		int statValue = Integer.parseInt(statValueString);
		asm.sendMessage(new StatsAnswerFromGUI(i,statValue), asm.getMyActorAddress());
	}

	private void jButtonFinalAnswerActionPerformed(java.awt.event.ActionEvent evt) {
		int i = 7;
		if(jRadioOption1.isSelected()){
			i = 0;
		}
		if(jRadioOption2.isSelected()){
			i = 1;	
		}
		if(jRadioOption3.isSelected()){
			i = 2;
		}
		if(jRadioOption4.isSelected()){
			i = 3;
		}
		asm.sendMessage(new FinalAnswerFromGUI(i),asm.getMyActorAddress());	
	}
    
    public void isTeamLeder(){
/*    	jComboBoxStat.setVisible(false);
    	jButtonPartialAnswer.setVisible(false);
        jButtonFinalAnswer.setText("Answer");
        jButtonFinalAnswer.setVisible(true);
    	jLabelLeader.setText("As a team leader you can submit the final answer for the group here ->");*/
    	
    	jComboBoxStat.setVisible(true);
    	jButtonPartialAnswer.setVisible(true);
        jButtonFinalAnswer.setText("Answer");
        jButtonFinalAnswer.setVisible(true);
    	jLabelLeader.setText("As a team leader you can submit the final answer for the group here ->");
    }
    
    public void isTeamMember(){
    	jButtonPartialAnswer.setText("Submit Answer");
    	jButtonFinalAnswer.setVisible(false);
        jLabelMember1.setText("As a regular team member you can only submit how ");
        jLabelMember2.setText("sure you are that this answer is correct");
        jLabelMember3.setText("Only the team leader can submit the final answer");
        jLabelMember4.setText("How sure are you?");
        
    }
    
    public DefaultListModel getJMembersStatsModel(){
    	return jMembersStatsModel;
    }
    
    public void setQuestion(Vector<SessionOption> options, Vector questions){
    
        jLabelOption1Stat.setToolTipText("No one have answered this alternative");
        
        jLabelOption2Stat.setToolTipText("No one have answered this alternative");
        
        jLabelOption3Stat.setToolTipText("No one have answered this alternative");
        
        jLabelOption4Stat.setToolTipText("No one have answered this alternative");
    	
    	//setting answeroptions
    	for (int i=0; i< options.size() ; i++){
    		if(i==0){
    			jRadioOption1.setText(((SessionOption)options.get(i)).getAnswerOption());
    			jLabelOption1Stat.setText("%");
    		}
    		if(i==1){
    			jRadioOption2.setText(((SessionOption)options.get(i)).getAnswerOption());
    			jLabelOption2Stat.setText("%");
    		}
    		if(i==2){
    			jRadioOption3.setText(((SessionOption)options.get(i)).getAnswerOption());
    			jLabelOption3Stat.setText("%");
    		}
    		if(i==3){
    			jRadioOption4.setText(((SessionOption)options.get(i)).getAnswerOption());
    			jLabelOption4Stat.setText("%");
    		}
    			
    	// 

    		
    	}
    	//setting questiontext
    	String all = new String();
    	for(int j=0; j<questions.size(); j++){
    		all += questions.get(j).toString();
    	}
    	jTextAreaQuestionText.setText(all);
    	
    }
    
    public void playerLockedQuestion(String name, int option, int stat){
    	String inputStat = name+" answered alternative "+option+" , "+stat+"%";
    	getJMembersStatsModel().addElement(inputStat);
    	if (option == 0){
    		tooltip1+= name + " was " +stat+"% sure about the answer<BR>";
    		jLabelOption1Stat.setToolTipText(tooltip1);
    	}
    	if (option == 1){
    		tooltip2+= name + " was " +stat+"% sure about the answer<BR>";
    		jLabelOption2Stat.setToolTipText(tooltip2);
    	}
    	if (option == 2){
    		tooltip3+= name + " was " +stat+"% sure about the answer<BR>";
    		jLabelOption3Stat.setToolTipText(tooltip3);
    	}
    	if (option == 3){
    		tooltip4+= name + " was " +stat+"% sure about the answer<BR>";
    		jLabelOption4Stat.setToolTipText(tooltip4);
    	}
    	
    }
    
    public void updateOptionStats(HashMap statsQuestions){
     Iterator<Entry> iterator = statsQuestions.entrySet().iterator();
  		
  		while(iterator.hasNext()){
  			Map.Entry<Integer, Integer> me = (Map.Entry)iterator.next();
  			if(me.getKey().equals(0)){
  				jLabelOption1Stat.setText(me.getValue().toString()+"%");
  			}
  			if(me.getKey().equals(1)){
  				jLabelOption2Stat.setText(me.getValue().toString()+"%");
  			}
  			if(me.getKey().equals(2)){
  				jLabelOption3Stat.setText(me.getValue().toString()+"%");
  			}
  			if(me.getKey().equals(3)){
  				jLabelOption4Stat.setText(me.getValue().toString()+"%");
  			}
  			
  			
  		}
    	
    }
    
    
    
}
